package com.itranlin.voddemo.core.controller;

import com.aliyuncs.AcsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import com.itranlin.voddemo.common.annotation.GetVodClient;
import com.itranlin.voddemo.common.bean.RequestResult;
import com.itranlin.voddemo.common.bean.StatusEnum;
import com.itranlin.voddemo.core.dto.vod.UploadInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/vod")
@Api(tags = {"阿里云点播相关接口"})
public class VodController {
    @PostMapping("/get-upload-addr")
    @ApiOperation(value = "获得上传凭证及上传地址及分配视频ID")
    public RequestResult createUploadVideo(@ApiIgnore @GetVodClient DefaultAcsClient client,@RequestBody @ApiParam(value = "视频信息")UploadInfo uploadInfo) throws ClientException {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        CreateUploadVideoResponse response;

        /*必选，视频源文件名称（必须带后缀, 支持 ".3gp", ".asf", ".avi", ".dat", ".dv", ".flv", ".f4v", ".gif", ".m2t", ".m3u8", ".m4v", ".mj2", ".mjpeg", ".mkv", ".mov", ".mp4", ".mpe", ".mpg", ".mpeg", ".mts", ".ogg", ".qt", ".rm", ".rmvb", ".swf", ".ts", ".vob", ".wmv", ".webm"".aac", ".ac3", ".acm", ".amr", ".ape", ".caf", ".flac", ".m4a", ".mp3", ".ra", ".wav", ".wma"）*/
        request.setFileName(uploadInfo.getVideoName());
        //必选，视频标题
        request.setTitle(uploadInfo.getTitle());
        //可选，分类ID
        request.setCateId(0);
        //可选，视频标签，多个用逗号分隔
        String tags = uploadInfo.getTags().stream().collect(Collectors.joining(","));
        request.setTags(tags);
        //可选，视频描述
        request.setDescription(uploadInfo.getDescription());
        response = client.getAcsResponse(request);
        return RequestResult.e(StatusEnum.OK,response);
    }

    @PostMapping("/refresh-upload-token")
    @ApiOperation(value = "根据视频ID刷新上传凭证")
    public RequestResult refreshUploadVideo(@ApiIgnore @GetVodClient DefaultAcsClient client, @RequestBody @ApiParam(value = "视频ID")  String videoId) throws ClientException {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        RefreshUploadVideoResponse response;
        request.setVideoId(videoId);
        response = client.getAcsResponse(request);
        return RequestResult.e(StatusEnum.OK,response);
    }


    @PostMapping("/get-play-addr")
    @ApiOperation(value = "根据视频ID获得播放地址")
    public RequestResult getVideoPlayAuth(@ApiIgnore @GetVodClient DefaultAcsClient client,@RequestBody  @ApiParam(value = "视频ID") String videoId) throws ClientException {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        GetVideoPlayAuthResponse response;
        response = client.getAcsResponse(request);
        return RequestResult.e(StatusEnum.OK,response);
    }

    @PostMapping("/get-video-info")
    @ApiOperation(value = "根据视频ID获得文件信息")
    public RequestResult getVideoInfo(@ApiIgnore @GetVodClient DefaultAcsClient client,@RequestBody  @ApiParam(value = "视频ID") String videoId) throws ClientException {
        GetMezzanineInfoRequest request = new GetMezzanineInfoRequest();
        request.setVideoId(videoId);
        //源片下载地址过期时间
        request.setAuthTimeout(3600L);
        AcsResponse response =  client.getAcsResponse(request);
        return RequestResult.e(StatusEnum.OK,response);
    }
}
